#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>

#define Wide 1200//窗口宽度
#define Height 600//窗口高度
#define MinRadius 1//半径单元
#define Point Wide/12//小球点位

void swap(int* p,int* q);
int main()
{
	initgraph(Wide,Height);
	cleardevice();
	
	int i,j,k;
	int Numbers[12];
	for(i=1;i<=11;i++)//随机生成11个数字并赋值给数组(初始化)
	{
		Numbers[i]=rand()%(Point*4/10)+Point/10;//限制数字大小（限制小球大小）
		fillcircle(i*Point,Height/3,Numbers[i]*MinRadius);//半径为1*数字大小
	}
	Sleep(100);
	
	for(k=1;k<=11;k++)
	{
		for(j=1;j<11;j++)
		{
			//正在比较的两个球变红
			clearcircle(j*Point,Height/3,50);
			clearcircle((j+1)*Point,Height/3,50);
			setfillcolor(RED);
			fillcircle(j*Point,Height/3,Numbers[j]*MinRadius);
			fillcircle((j+1)*Point,Height/3,Numbers[j+1]*MinRadius);
		
			Sleep(500);//停顿500ms

			if(Numbers[j]>Numbers[j+1])//比较与交换
			{
				swap(&Numbers[j],&Numbers[j+1]);
			}
			
			if(j>1&&j<10)
			{
				//把上一对球变回白色
				clearcircle((j-1)*Point,Height/3,50);
				clearcircle(j*Point,Height/3,50);
				setfillcolor(WHITE);
				fillcircle((j-1)*Point,Height/3,Numbers[j-1]*MinRadius);
				fillcircle(j*Point,Height/3,Numbers[j]*MinRadius);
			}
			
		}
			//循环末尾补充：最后两个小球变回白色
			clearcircle(10*Point,Height/3,50);
			clearcircle(11*Point,Height/3,50);
			setfillcolor(WHITE);
			fillcircle((10)*Point,Height/3,Numbers[j-1]*MinRadius);
			fillcircle(11*Point,Height/3,Numbers[j]*MinRadius);
	}

	_getch();

	return 0;
}
void swap(int* p,int* q)//交换函数
{
	int temp;
	temp=*p;
	*p=*q;
	*q=temp;
}